<template>
  <div style="height:100%;">
    <el-row>
      <el-col :span="8">
        <el-input placeholder="请输入关键字" prefix-icon="el-icon-search" v-model="name"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="addDict"></el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="name" label="表名"></el-table-column>
        <el-table-column prop="code" label="编码"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="enable" label="状态">
          <template slot-scope="scope">
            <el-button
              size="mini"
              plain
              @click.stop="() => enable(scope.row)"
            >{{scope.row.enable == 1 ? "发布":"禁用"}}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button-group>
              <el-button size="mini" icon="el-icon-edit" @click.stop="() => edit(scope.row)"></el-button>
              <el-button
                size="mini"
                icon="el-icon-edit-outline"
                @click.stop="() => editValue(scope.row)"
              ></el-button>
              <el-button size="mini" icon="el-icon-delete" @click.stop="() => del(scope.row)"></el-button>
            </el-button-group>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total"
        background
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
export default {
  props: {
    dirId: {
      type: Number,
      required: true
    },
    dirName: {
      type: String,
      required: true
    }
  },
  watch: {
    dirId(val) {
      this.loadDictData();
    },
    name(val) {
      this.loadDictData();
    }
  },
  data() {
    return {
      name: null,
      pageSize: 5,
      pageNum: 1,
      total: 0,
      tableData: []
    };
  },
  methods: {
    enable(row) {
      let _this = this;
      this.axios
        .post(this.HOST + "/table/enable", {
          id: row.id,
          status: row.enable == 1 ? 0 : 1
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          debugger;
          row.enable = res.data.data.enable;
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    addDict() {
      this.$router.push({
        path: "/dict/0",
        query: {
          dirId: this.dirId,
          dirName: this.dirName
        }
      });
    },
    edit(row) {
      this.$router.push({
        path: "/dict/" + row.id,
        query: {
          dirId: this.dirId,
          dirName: this.dirName
        }
      });
    },
    editValue(row) {
      this.$router.push("/value/" + row.id);
    },
    del(row) {
      let _this = this;
      this.axios
        .post(this.HOST + "/table/delete", {
          id: row.id
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          _this.loadDictData();
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.loadDictData();
    },
    loadDictData() {
      let _this = this;
      this.axios
        .post(this.HOST + "/table/page", {
          dirId: this.dirId,
          pageSize: this.pageSize,
          pageNum: this.pageNum,
          name: this.name
        })
        .then(function(res) {
          if (!res.data.status) {
            _this.$alert(res.data.msg, "提示", {
              confirmButtonText: "确定"
            });
            return;
          }
          debugger;
          _this.tableData = res.data.data.datas;
          _this.total = res.data.data.totalCount;
        })
        .catch(function(err) {
          _this.$message.error("操作失败" + err);
        });
    }
  }
};
</script>
<style >
</style>
